﻿using System;
using System.Linq;
using System.Diagnostics;
using CodeFirst.Data;

namespace CodeFirst.Test
{
    static class SimpleTest
    {
        private static void Load(this BlogContext db)
        {
            int counter = 0;
            foreach (var blog in db.Blogs)
            {
                ++counter;
                Console.WriteLine("[{0,2}] {1}", counter, blog.Title);
            }
        }

        private static void Insert(this  BlogContext db, int number)
        {
            var newBlogs = from index in Enumerable.Range(1, number)
                           select new Blog
                                      {
                                          Title = string.Format("blog-{0}", index),
                                          Owner = string.Format("stasi#{0}", index)
                                      };

            foreach (var blog in newBlogs)
            {
                db.Blogs.Add(blog);
            }
            db.SaveChanges();
        }

        public static void TestMain()
        {
            var sw = new Stopwatch();
            using (var db = new BlogContext())
            {
                int number = 10;
                float tmcost = sw.TimeCost(() => db.Insert(number));
                Console.WriteLine("inserting {0} records cost {1} seconds", number, tmcost);

                tmcost = sw.TimeCost(() => db.Load());
                Console.WriteLine("Loading costs {0} seconds", tmcost);
            }//using
        }// TestMain
    }
}
